package com.liqw.fastswing.ui.view;

import javax.swing.*;

/**
 * This view is for displaying or writing a Journal Entry.  Just a simple JTextArea with an action button for either
 * saving the record or closing the view.
 */
public abstract class UserView {
    private JPanel panel;
    private JTextArea textArea;
    private JButton saveButton;

    protected UserView() {
        saveButton.addActionListener(e -> saveButtonClicked());
    }

    protected abstract void saveButtonClicked();

    public void setText(String text) {
        textArea.setText(text);
    }

    public String getText() {
        return textArea.getText();
    }

    /**
     * Enable View Mode, which just makes the JTextArea non-editable and sets the text of the Action Button to "Close."
     */
    public void viewMode() {
        textArea.setEditable(false);
        saveButton.setText("关闭");
    }

    public void writeMode() {
        textArea.setEditable(true);
        saveButton.setText("保存");
    }

    /**
     * @noinspection ALL
     */
    public JComponent getRootComponent() {
        return panel;
    }
}
